package com.zjpavt.client.page;

import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.page.pageunit.ScreenLine;
import com.zjpavt.client.page.abstractpage.AbstractBasePage;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 三代机中删除的一页，暂时用不到了
 */
public class LoopCurrentPage extends AbstractBasePage {
    private final static int MAX_LENGTH = 3;
    private final static String INVALID_VALUE = "**************";

    private final Pattern m = Pattern.compile("CH[0-9]{2}\\[[ABC][0-9]{3}_[0-9]{3}]");

    public LoopCurrentPage(int pageNum,long mRefreshPeriod) {
        super(pageNum,mRefreshPeriod);
    }

    @Override
    public void refreshPage() {
        String msg = "CH01[A001_001] CH02[A002_002] CH03[A003_003]";
        LedScreen.getInstance().setText(getPageContext(msg.split(" ")));
    }

    @Override
    public void close() {

    }

    private List<ScreenLine> getPageContext(String[] channels) {
        List<ScreenLine> result = new ArrayList<>();
        result.add(new ScreenLine.ScreenLineBuilder().pageNum(getPageNum()).build());
        int length;
        if (channels.length > MAX_LENGTH) {
            length = MAX_LENGTH;
        } else {
            length = channels.length;
        }
        String text;
        for (int i = 0; i < length; i++) {
            if (m.matcher(channels[i]).matches()) {
                text = channels[i];
            } else {
                text = INVALID_VALUE;
            }
            result.add(new ScreenLine.ScreenLineBuilder().text(text).build());
        }
        return result;
    }
}
